package com.wlf.admin.system.controller;

import cn.hutool.http.HtmlUtil;
import com.wlf.admin.common.util.AjaxBean;
import com.wlf.admin.common.util.JSON;
import com.wlf.admin.system.domain.SysRole;
import com.wlf.admin.common.dto.ParamPage;
import com.wlf.admin.system.service.SysMenuService;
import com.wlf.admin.system.service.SysRoleService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;

@RestController
@RequestMapping("/role")
public class RoleController {
    @Resource
    private SysMenuService menuService;
    @Resource
    private SysRoleService roleService;


    @GetMapping("/main")
    ModelAndView indexHtml() {
        ModelAndView view = new ModelAndView("role/index");
        view.addObject("menuTree",JSON.toJSONString(menuService.menuTree()));
        return view;
    }


    @PostMapping("/list")
    AjaxBean list(SysRole role, ParamPage paramPage) {
        return roleService.getList(role, paramPage);
    }

    @PostMapping("/add")
    AjaxBean add(SysRole role) {
        return roleService.add(role);
    }

    @GetMapping("/addForm")
    ModelAndView addFormHtml() {
        return new ModelAndView("role/add");
    }

    @GetMapping("/editForm")
    ModelAndView editFormHtml(Long id) {
        ModelAndView view = new ModelAndView("role/edit");
        view.addObject("role", roleService.get(id));
        return view;
    }

    @GetMapping("/getMenuIdById")
    AjaxBean getMenuIdById(Long id) {
        return roleService.getMenuIdById(id);
    }

    @PostMapping("/saveRoleMenu")
    AjaxBean saveRoleMenu(Long roleId, String menuIds) {
        return roleService.saveRoleMenu(roleId, JSON.parseArray(HtmlUtil.unescape(menuIds), Long.class));
    }

    @PostMapping("/update")
    AjaxBean update(SysRole role) {
        return roleService.update(role);
    }

    @GetMapping("/del")
    AjaxBean del(Long id) {
        return roleService.del(id);
    }
}
